package com.komodo.island.gdx.screen;

import android.sax.StartElementListener;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.komodo.island.gdx.Art;
import com.komodo.island.gdx.Constants;
import com.komodo.island.gdx.Input;
import com.komodo.island.gdx.KomodoIsland;
import com.komodo.island.gdx.dialog.Dialog;
import com.komodo.island.gdx.dialog.LoadingDialog;
import com.komodo.island.gdx.dialog.PauseDialog;

public abstract class Screen implements com.badlogic.gdx.Screen {

	/*
	 * Base class for all screen in this game
	 */

	/*
	 * This ID will help to differ one screen to another...especially for
	 * handling input, so when you derive screen, please fill this ID with Input
	 * constant
	 */

	public int ID;

	private int accum;

	public static final int RUNNING = 1;
	public static final int PAUSED = 0;
	public static final int LOADING = 2;

	public KomodoIsland komodoIsland;
	protected OrthographicCamera camera;
	protected SpriteBatch batch;
	protected Dialog pauseDialog;
	protected Dialog loadingDialog;

	public Texture texture;
	protected Music bgm;

	protected int screenState = LOADING;

	public Screen previousScreen;

	public Rectangle glViewport;

	/*
	 * You should init the textures too
	 */
	public void init(KomodoIsland komodoIsland, Texture texture) {
		this.komodoIsland = komodoIsland;

		// camera = new OrthographicCamera(Gdx.graphics.getWidth(),
		// Gdx.graphics.getHeight());
		
		
		camera = new OrthographicCamera(480, 320);

//		 camera = this.komodoIsland.camera;

		camera.position.set(480 / 2, 320 / 2, 0);
		// camera.position.set(Gdx.graphics.getWidth()/2,
		// Gdx.graphics.getHeight()/2,0);

		batch = komodoIsland.batch;
		// texture = new Texture(Gdx.files.internal(textureName));
		this.texture = texture;
		// Matrix4 projection = new Matrix4();
		// projection.setToOrtho(0, 480, 320, 0, -1, 1);
		// batch.setProjectionMatrix(projection);

		glViewport = new Rectangle(0, 0, 480, 320);

	}

	public void dispose() {
		// TODO Auto-generated method stub

	}

	public void cleanResScreen() {
		try {
//			pauseDialog.dispose();
//			loadingDialog.dispose();
//			
//			
		} catch (Exception e) {

		}
	}

	public void hide() {
		// TODO Auto-generated method stub
//		try {
//			pauseDialog.dispose();
//			loadingDialog.dispose();
//		} catch (Exception e) {
//
//		}
//		pauseDialog = null;
//		loadingDialog = null;

		if (bgm != null && bgm.isPlaying())
			bgm.stop();
	}

	public void pause() {
		// TODO Auto-generated method stub

	}

	public void render(float arg0) {
		// TODO Auto-generated method stub
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		// GL10 gl = Gdx.graphics.getGL10();

		// Camera --------------------- /
		// gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		// gl.glViewport((int) glViewport.x, (int) glViewport.y,
		// (int) glViewport.width, (int) glViewport.height);
		camera.update();
		// camera.apply(Gdx.gl);

		// camera.apply(Gdx.graphics.getGL10());
		batch.setProjectionMatrix(camera.combined);

		if (screenState == RUNNING) {
			onScreenRun();
		} else if (screenState == PAUSED) {
			onScreenPause();
		} else if (screenState == LOADING) {
			if (accum >= 2 && accum > 0) {
				
				
				onScreenLoading();

				setScreenToRun();
				accum = -1;
			} else {
				accum++;
			}
			if (loadingDialog != null)
				loadingDialog.draw();
		}

	}

	public void setScreenToRun() {
		setCameraToDefault();
		screenState = RUNNING;
		komodoIsland.input.removeStage();
	}

	public void setScreenToPause() {
		setCameraToDefault();
		screenState = PAUSED;
		komodoIsland.input.setStage(pauseDialog);
	}

	public void setScreenToLoading() {
		setCameraToDefault();
		screenState = LOADING;
		accum = -1;
		komodoIsland.input.removeStage();
	}

	/*
	 * Make the camera back to normal
	 */

	public void setCameraToDefault() {
		camera.position.set(480 / 2, 320 / 2, 0);
		// camera.position.set(Gdx.graphics.getWidth()/2,
		// Gdx.graphics.getHeight()/2,0);
	}

	public abstract void onScreenRun();

	public void onScreenPause() {
		pauseDialog.draw();
	}

	public abstract void onScreenLoading();

	/*
	 * To exit or back to previous screen
	 */

	public void exitScreen() {
		
		if (previousScreen != null)
			komodoIsland.goToScreen(previousScreen);
	}

	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub
		// camera = new OrthographicCamera(arg0, arg1);
		// camera.position.set(Gdx.graphics.getWidth()/2,
		// Gdx.graphics.getHeight()/2,0);
		// setCameraToDefault();
	}

	public void resume() {
		// TODO Auto-generated method stub

	}

	public void show() {
		// TODO Auto-generated method stub
		// System.out.println("Show" + screenState);
//		if (pauseDialog == null)
//			this.pauseDialog = new PauseDialog(Constants.GAME_WIDTH,
//					Constants.GAME_HEIGHT, true, this,
//					komodoIsland.dialogTexture);
//		if (loadingDialog == null)
//			this.loadingDialog = new LoadingDialog(
//					Constants.GAME_WIDTH, Constants.GAME_HEIGHT, true,
//					this, komodoIsland.dialogTexture);
		if (pauseDialog == null)
			this.pauseDialog = komodoIsland.pauseDialog;
		pauseDialog.setScreen(this);
		
		if (loadingDialog == null)
			this.loadingDialog = komodoIsland.loadingDialog;
		
		
		if (bgm != null)
			bgm.play();
		// if (screenState == LOADING) {
		// loadingDialog.draw();
		// }

	}

}
